// pages/refund/index.js
const app     = getApp()
const request = require('../../utils/request.js');
const config = require('../../utils/config.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        typeArr: ['A', 'B'],
        typeIdx: 0,
        refundReason: [ '7天无理由','其他'],
       
        images:[],
        order_id:0,
        item_id:0,
    },
    typeChange(e) {
        this.setData({
            typeIdx: e.detail.value
        })
    },
    deleteimg(e){
       var  index = e.currentTarget.dataset.index;
       var arr= this.data.images;
    arr.splice(index,1)
    console.log(11)
   
    this.setData({
      images: arr,
    })
    },
    // 删除时间段
  dele_time: function (e) {
    console.log('删除')
    console.log(e.currentTarget.id)
    var id = e.currentTarget.id;
    
  },
    bindReasonPickerChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
          reasonIndex: e.detail.value
        })
      },
      input(e){
       this.setData({
        content:e.detail.value
       })
      },
      submit(){
        if(!this.data.reasonIndex){
            wx.showToast({
              title: '请选择申请退款原因',
              icon:'none'
            })
            return false
        }
        if(!this.data.content){
            wx.showToast({
                title: '请填写申请说明',
                icon:'none'
              })
              return false
        }
        // if(this.data.images.length==0){
        //     wx.showToast({
        //         title: '请上传图片',
        //         icon:'none'
        //       })
        //       return false
        // }
        var that = this;
        var json = {};
       
       json.order_id=that.data.order_id;
      // json.item_id=that.data.item_id;
       json.reason=that.data.refundReason[that.data.reasonIndex] ;
       json.content=that.data.content;
       json.images=JSON.stringify(that.data.images);
        request.requestLoading(
            "/api/aftersales/addaftersales",
            json,
            'POST',
          ).then((res) => {
           
            if(res.code==1){
                getApp().globalData.is_need_refresh=true;
                wx.navigateBack({
                    delta:1
                })
            }
           
      
          }).catch((err) => {
            console.error(err);
          });
      },
    reasonChange(e) {
        console.log(e.detail.value)
        this.setData({
            reasonIdx: e.detail.value
        })
    },
    uploadImg(e){
        var that = this;
          
        wx.chooseMedia({
          count: 9-that.data.images.length,
          mediaType: ['image'],
          sourceType: ['album', 'camera'],
          maxDuration: 30,
          camera: 'back',
          success(res) {
              console.log(res)
            let tempFilePaths = res.tempFiles;
            for (let i = 0; i < tempFilePaths.length; i++) {
                console.log(tempFilePaths[i]['tempFilePath']);
               
                    wx.uploadFile({
                        url: config.web_url + '/api/common/ossupload',
                        filePath: tempFilePaths[i]['tempFilePath'],
                        name: 'file',
                        header: {
                            "Content-Type": "multipart/form-data",
                            'token': wx.getStorageSync('token')
                        },
                        success: function (res) {
                            var data = JSON.parse(res.data);
                            console.log(data)
                          
                            if(res.statusCode==200){ 
                              var images =  that.data.images;
                              console.log(data.data.fullurl)
                              images.push(data.data.fullurl)
                                that.setData({
                                   images:images
                                })
                               
                            }else{
                                wx.showToast({
                                  title: data.msg,
                                  icon:'none'
                                })
                            }
                           
                           
                        },
                        fail:function(res){
                            console.log(res)
                        }
                    })
                }
          },fail:function(res){
              console.log(res)
          }
        })    
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            order_id:options.order_id,
            item_id:options.item_id
        })
        this.getDetail()
    },
    getDetail()
    {
        var that = this;
        var json = {};
       
       json.order_id=that.data.order_id;
      // json.item_id=that.data.item_id
        request.requestLoading(
            "/api/aftersales/orderitemDetail",
            json,
            'GET',
          ).then((res) => {
           
        that.setData({
            order_detail:res.data,
        })
           
      
          }).catch((err) => {
            console.error(err);
          });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})